# @Time : 2021/6/11 11:01

# @Author : zhujianhao

# @File : locust_baidu.py 

# @Software: PyCharm

from locust import HttpLocust, TaskSet, task
import random
from faker import Faker
from locust import HttpUser, task, between,HttpLocust,User
from locust import LoadTestShape
import requests
from requests.packages.urllib3.exceptions import InsecureRequestWarning
requests.packages.urllib3.disable_warnings(InsecureRequestWarning)
fake = Faker(locale='zh_CN')
import json

# def fakertime():
#     return fake.unix_time()
#
#
# def fakername():
#     return fake.company() + str('的订单') + str(random.randint(0, 999))
#
# def random_page():
#     return random.randint(1,1360)

class QuickstartUser(TaskSet):
    def on_start(self):
        pass

    @task(1)
    def api3(self):
        header = {
            "Host": "scrm-test.tangees.com",
            "Connection": "keep-alive",
            # "Content-Length": "482",
            "Accept": "*/*",
            "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36 Edg/89.0.774.63",
            # "Content-Type": "application/json;charset=UTF-8",
            "Origin": "https://scrm-test.tangees.com",
            "Sec-Fetch-Site": "same-origin",
            "Sec-Fetch-Mode": "cors",
            "Sec-Fetch-Dest": "empty",
            "Referer": "https://scrm-test.tangees.com/customer-migration/resign-inheritance/group",
            "Accept-Encoding": "gzip, deflate, br",
            "Accept-Language": "zh-CN,zh;q=0.9",
            "Cookie": "accountCenterSessionId=.eJw9j8FOwzAQRP_F5x7Wa-_G7rm0F1IEaoXCJfLaGyU0BKkpIIr4dyKQOD5pZjTvy7RDMWuDHGPkEmOqAleWsgX16KwEhMg5ZBfQY8xoGZgokSVizy5XrmPJaelm1cDEaqH4DJkLBUEqWIqAkqJzTsSpIgSbyZGvRIv3Gl0CZ1Ym92madFzOfKgsPKmWdk7v2l5e2yJm3aVx1pVpu7PO_T--zXr-c2CwlSbvobKSMoh4WVRKWLbm30BzOGF9vYe7zenydKg_6wFgjzd0-7gd97vjpd48vDR4hObaP9e77WC-fwDTM1N1.E6RkSg.jZAZ3bhLEpMluQ5KLRY8invQOEU",
        }

        param = {'pageSize': 10, 'pageNum': 1}
        with self.client.get(url='https://scrm-test.tangees.com/test/web/inherit/offline/group/list', headers=header, verify=False, params=param,catch_response=True) as response:
            if response.status_code == 200:
                if json.loads(response.text)['errMsg'] == 'success！':
                    pass
                else:
                    response.failure("Timeout")
            else:
                response.failure("No data")

    def on_stop(self):
        pass


# class StagesShape(LoadTestShape):
#     '''
#     在不同的阶段 具有不同的用户数和 产生率的 图形形状
#
#
#     参数解析:
#         stages ：字典列表，每个字典都具有下列这些键值的阶段:
#             duration -- 持续时间，  经过多少秒后，进入到下个阶段
#             users -- 总用户数
#             spawn_rate -- 产生率，即每秒启动/停止的用户数
#             stop -- 可以在特定阶段停止测试的值
#         stop_at_end -- 可以在所有阶段设置运行后停止
#
#     '''
#
#     stages = [
#         {"duration": 300, "users": 10, "spawn_rate": 10},
#         {"duration": 600, "users": 20, "spawn_rate": 10},
#         {"duration": 900, "users": 50, "spawn_rate": 20}
#     ]

    # def tick(self):
    #     run_time = self.get_run_time()
    #
    #     for stage in self.stages:
    #         if run_time < stage['duration']:
    #             tick_data = (stage['users'],stage['spawn_rate'])
    #             return tick_data
    #
    #     return None

class WebsiteUser(HttpUser):
    tasks = [QuickstartUser]
    host = "https://scrm-test.tangees.com"
    wait_time = between(0.1,0.5)#多少秒用户发起1次请求




if __name__ == "__main__":
    import os

    os.system("locust -f locust_baidu.py --web-host=127.0.0.1")